<?php
namespace Api\Controller;

use Think\Controller;
use Base\FrontController;
use Think\ImageNew;

/**
 * @name 前端衣橱相关功能提交和查询数据 ，访问本接口下任何方法均要求session中已保留注册用户信息
 * swagger-ui api文档生成cmd： php D:\work\php\swagger-php\vendor\zircote\swagger-php\bin\swagger D:\work\php\wx-third-platform\code\Application\Api\Controller\swagger D:\work\php\wx-third-platform\code\Application\Api\Controller\ApiBHClosetController.class.php  -o D:\work\php\wx-third-platform\code\swui\衣橱-closet.json
 * @author perry
 *
 */
class ApiBHClosetController extends FrontController
{

    // private $weixin_user;

    function _initialize()
    {
        parent::_initialize();
        
        $this->closetM=D('Home/BHCloset');
        $this->uid=$this->weixin_user['id'];
        $this->imgSaveSubFolder="closet";
    }

     /**
     * @SWG\Get(
     *     path="/ApiBHCloset/getClosetListByPage",
     *     tags={"getClosetListByPage"},
     *     description="分页获取当前用户的衣橱列表数据",
     *     operationId="getClosetListByPage",
     *     produces={"application/json", "application/xml", "text/xml", "text/html"},
     *     @SWG\Parameter(
     *         name="t",
     *         in="query",
     *         description="分类id(来自接口/Api/BHType/getAll)",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Parameter(
     *         name="p",
     *         in="query",
     *         description="指定页码(缺省为1)",
     *         required=false,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Parameter(
     *         name="ps",
     *         in="query",
     *         description="每页容量(缺省为10)",
     *         required=false,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="当前登录用户的衣橱每页数据",
     *         @SWG\Schema(
     *             ref="#/definitions/closets"
     *         ),
     *     ),
     *     @SWG\Response(
     *         response="default",
     *         description="接口调用出错",
     *         @SWG\Schema(
     *             ref="#/definitions/ErrorModel"
     *         )
     *     )
     * )
     */
    public function getClosetListByPage(){
        $type=I('t',1);
        $page=I('p',1);
        $pagesize=I('ps',10);
        //getByPage($uid=0,$type=1,$status=1,$name=false,$page=1,$pageSize=10,$field=true){
        $re=$this->closetM->getByPage('closet',$this->uid,$type,1,false,$page,$pagesize,'id,uid,type,img,name,memo');
        // dump($re);
        $this->customReturn(1,$re);
        
    }

    /**
     * @SWG\Get(
     *     path="/ApiBHCloset/deleteOne",
     *     tags={"BHCloset-deleteOne"},
     *     description="删除当前用户拥有的1条衣橱数据",
     *     operationId="BHCloset-deleteOne",
     *     produces={"application/json", "application/xml", "text/xml", "text/html"},
     *     @SWG\Parameter(
     *         name="id",
     *         in="query",
     *         description="待删除数据主键id",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     
     *     @SWG\Response(
     *         response=200,
     *         description="删除成功与否结果",
     *         
     *     ),
     *     @SWG\Response(
     *         response="default",
     *         description="接口调用出错",
     *         @SWG\Schema(
     *             ref="#/definitions/ErrorModel"
     *         )
     *     )
     * )
     */
    public function deleteOne(){
        $id=I('id',-1);
        try {
            $re=$this->closetM->deleteOne($this->uid,$id);
            $this->customReturn(1,$re);
            
        } catch (\Exception $exc) {
            $succ = $exc->getCode() == 1 ? -1 : $exc->getCode();
            $datas = array('errcode' => $succ, 'errmsg' => $exc->getMessage());
            $this->customReturn($succ, $datas);
        }
        
        
    }

    /**
     * @SWG\Get(
     *     path="/ApiBHCloset/addCloset",
     *     tags={"BHCloset-addCloset"},
     *     description="新增一条衣橱数据",
     *     operationId="BHCloset-addCloset",
     *     produces={"application/json", "application/xml", "text/xml", "text/html"},
     *     @SWG\Parameter(
     *         name="type",
     *         in="query",
     *         description="待新增衣橱所属子类(必须为数字，大衣，配饰等类别唯一id,如不传默认1)",
     *         required=false,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Parameter(
     *         name="name",
     *         in="query",
     *         description="待新增衣橱的名称",
     *         required=false,
     *         type="string"
     *     ), 
     *     @SWG\Parameter(
     *         name="memo",
     *         in="query",
     *         description="待新增衣橱的附注",
     *         required=false,
     *         type="string"
     *     ), 
     *     @SWG\Parameter(
     *         name="closet_pic",
     *         in="query",
     *         description="待新增衣橱的去背图base64编码后字符串",
     *         required=true,
     *         type="string"
     *     ), 
     *     @SWG\Response(
     *         response=200,
     *         description="新增衣橱结果数组",
     *         @SWG\Schema(
     *             ref="#/definitions/closet"
     *         )
     *         
     *     ),
     *     @SWG\Response(
     *         response="default",
     *         description="接口调用出错",
     *         @SWG\Schema(
     *             ref="#/definitions/ErrorModel"
     *         )
     *     )
     * )
     */
    public function addCloset(){
        try {
            $newClosEtdata=array();
            $newClosEtdata['create_time']=time();
            $newClosEtdata['update_time']=$newClosEtdata['create_time'];
            //确定衣橱所属用户
            $newClosEtdata['uid']=$this->uid;
            // //确定来源
            // if(in_array($this->uid, C('super_style_uid'))){
            //     //是管理员在新增造型，则分类为2管理员的造型灵感
            //     $newClosEtdata['cate']=2;
            // }
            // else {
            //     //普通用户新增造型
            //     $newClosEtdata['cate']=1;
            // }
            //确定所属分类(大衣，下衣等)
            $label=I('type');
            if(ic_isPosInt($label)){
                $newClosEtdata['type']=$label;
            }
            else {
                $newClosEtdata['type']=1;
            }
            //确定造型的组成元素id
            //$item_ids_str=I('item_ids',"");
            //$newClosEtdata['item_ids'] = $this->looksM->filterIdStr($item_ids_str);
            //状态默认为启用
            $newClosEtdata['status']=1;
            //确定名称
            $newClosEtdata['name']=ic_filterStr(I('name',""));
            //确定附注
            $newClosEtdata['memo']=ic_filterStr(I('memo',""));
            //确定上传的扣背图数据
            //测试base64图像 
            $rePic=I('closet_pic');
            //检查上传数据是否为有效图片文件
            $this->checkImgFile($rePic);
            //将base64编码的图片内容还原成jpg图片保存
            // $rePic=str_replace("data:image/png;base64,", "", $rePic);
            $startPos=stripos($rePic, ";base64,");
            if($startPos<=0){
                throw new \Exception("上传图片格式无效(!base64),请重试或联系管理员", -215);
            }
            $rePic=substr($rePic, $startPos+8);
            $rePic=base64_decode($rePic);
            // $this->checkImgFile($rePic); 这里再检查会返回false
            $len=strlen($rePic);
            $serverPath=ic_getUploadImgServerPath($this->imgSaveSubFolder,$newClosEtdata['create_time']);
            if(!is_dir($serverPath)) {
                // 检查目录是否编码后的
                if(is_dir(base64_decode($serverPath))) {
                    $serverPath   =   base64_decode($serverPath);
                }else{
                    // 尝试创建目录
                    if(!mkdir($serverPath)){
                        throw new \Exception("保存目录不存在，请联系管理员", -215);
                    }
                }
            }else {
                if(!is_writeable($serverPath)) {
                   throw new \Exception("保存目录不可写，请联系管理员", -333);
                }
            }
            //生成图片文件名
            $saveName=$this->uid."_".ic_micro_time().".png";
            $bytes=file_put_contents($serverPath.$saveName, $rePic);
            if($bytes!=$len){
                throw new \Exception("保存图片失败，请稍候再试".$len, -44);
            }
            //生成m图和s图
            ImageNew::thumb($serverPath.$saveName,$serverPath."s_".$saveName,'',354,395,true);
            ImageNew::thumb($serverPath.$saveName,$serverPath."m_".$saveName,'',708,790,true);
            $newClosEtdata['img']=$saveName;
            //插入数据库
            $newId=$this->closetM->add($newClosEtdata);
            $newClosEtdata['id']=$newId;
            //把图片组装成前端可以直接访问的http全路径
            $newClosEtdata['img']=ic_getUploadImgFullPath($this->imgSaveSubFolder,$newClosEtdata['img'],$newClosEtdata['create_time']);
            $this->customReturn(1,$newClosEtdata);
        } catch (\Exception $exc) {
            $succ = $exc->getCode() == 1 ? -1 : $exc->getCode();
            $datas = array('errcode' => $succ, 'errmsg' => $exc->getMessage());
            $this->customReturn($succ, $datas);
        }  
    }

    /**
     * @SWG\Get(
     *     path="/ApiBHCloset/updateCloset",
     *     tags={"BHCloset-updateCloset"},
     *     description="编辑并保存一条衣橱数据",
     *     operationId="BHCloset-updateCloset",
     *     produces={"application/json", "application/xml", "text/xml", "text/html"},
     *     @SWG\Parameter(
     *         name="id",
     *         in="query",
     *         description="待更新衣橱主键唯一id",
     *         required=true,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Parameter(
     *         name="type",
     *         in="query",
     *         description="待更新衣橱所属分类id",
     *         required=false,
     *         type="integer",
     *         format="int32"
     *     ),
     *     @SWG\Parameter(
     *         name="name",
     *         in="query",
     *         description="待更新衣橱的名称",
     *         required=false,
     *         type="string"
     *     ), 
     *     @SWG\Parameter(
     *         name="memo",
     *         in="query",
     *         description="待更新衣橱的附注",
     *         required=false,
     *         type="string"
     *     ), 
     *     @SWG\Parameter(
     *         name="closet_pic",
     *         in="query",
     *         description="待更新衣橱图片的base64编码后字符串",
     *         required=false,
     *         type="string"
     *     ), 
     *     @SWG\Response(
     *         response=200,
     *         description="更新成功后本衣橱最新数据数组",
     *         @SWG\Schema(
     *             ref="#/definitions/closet"
     *         )
     *         
     *     ),
     *     @SWG\Response(
     *         response="default",
     *         description="接口调用出错",
     *         @SWG\Schema(
     *             ref="#/definitions/ErrorModel"
     *         )
     *     )
     * )
     */
    public function updateCloset(){
        try {
            $id=I('id',-1);
            if(!ic_isPosInt($id)){
                throw new \Exception("待更新衣橱不存在,请检查", -427);  
            }
            $exist=$this->closetM->where("id=".$id)->find();
            if(!$exist){
                throw new \Exception("待更新衣橱不存在,请刷新页面重试", -431); 
            }
            if($exist['uid']!=$this->uid){
                throw new \Exception("您无权编辑他人的衣橱，请检查", -431);
            }
            $newClosEtdata=array();
            $newClosEtdata['update_time']=time();
            $newClosEtdata['create_time']=$exist['create_time'];
            //确定所属用户
            $newClosEtdata['uid']=$this->uid;
            //确定造型来源
            // if(in_array($this->uid, C('super_style_uid'))){
            //     //是管理员在新增造型，则分类为2管理员的造型灵感
            //     $newClosEtdata['cate']=2;
            // }
            // else {
            //     //普通用户新增造型
            //     $newClosEtdata['cate']=1;
            // }
            //确定所属类别
            $label=I('type');
            if(ic_isPosInt($label)){
                $newClosEtdata['type']=$label;
            }
            else {
                $newClosEtdata['type']=$exist['type'];
            }
            //确定造型的组成元素id
            // $item_ids_str=I('item_ids',"");
            // if(ic_checkUnEmpty($item_ids_str)){
            //     $newClosEtdata['item_ids'] = $this->looksM->filterIdStr($item_ids_str);
            // }
            // else {
            //     $newClosEtdata['item_ids']=$exist['item_ids'];
            // }
            //编辑时状态不可再更改
            // $newClosEtdata['status']=1;
            //确定名称
            $newClosEtdata['name']=ic_filterStr(I('name',""));
            if(!ic_checkUnEmpty($newClosEtdata['name'])){
                $newClosEtdata['name']=$exist['name'];
            }
            //确定附注
            $newClosEtdata['memo']=ic_filterStr(I('memo',""));
            if(!ic_checkUnEmpty($newClosEtdata['memo'])){
                $newClosEtdata['memo']=$exist['memo'];
            }
            //确定上传的合成图数据
            //测试base64图像 
            $rePic=I('closet_pic','');
            if(ic_checkUnEmpty($rePic)){
                //检查上传数据是否为有效图片文件
                $this->checkImgFile($rePic);
                //将base64编码的图片内容还原成jpg图片保存
                // $rePic=str_replace("data:image/jpeg;base64,", "", $rePic);
                // $rePic=substr($rePic, 22);
                $startPos=stripos($rePic, ";base64,");
                if($startPos<=0){
                    throw new \Exception("上传图片格式无效(!base64),请重试或联系管理员", -215);
                }
                $rePic=substr($rePic, $startPos+8);
                $rePic=base64_decode($rePic);
                // $this->checkImgFile($rePic); 这里再检查会返回false
                $len=strlen($rePic);
                $serverPath=ic_getUploadImgServerPath($this->imgSaveSubFolder,$newClosEtdata['create_time']);
                if(!is_dir($serverPath)) {
                    // 检查目录是否编码后的
                    if(is_dir(base64_decode($serverPath))) {
                        $serverPath   =   base64_decode($serverPath);
                    }else{
                        // 尝试创建目录
                        if(!mkdir($serverPath)){
                            throw new \Exception("保存目录不存在，请联系管理员", -215);
                        }
                    }
                }else {
                    if(!is_writeable($serverPath)) {
                       throw new \Exception("保存目录不可写，请联系管理员", -333);
                    }
                }
                //生成图片文件名
                $saveName=$this->uid."_".ic_micro_time().".png";
                $bytes=file_put_contents($serverPath.$saveName, $rePic);
                if($bytes!=$len){
                    throw new \Exception("保存衣橱图片失败，请稍候再试".$len, -44);
                }
                //生成m图和s图
                ImageNew::thumb($serverPath.$saveName,$serverPath."s_".$saveName,'',354,395,true);
                ImageNew::thumb($serverPath.$saveName,$serverPath."m_".$saveName,'',708,790,true);
                $newClosEtdata['img']=$saveName;
                //删除老图
                $this->delMulti($serverPath,$exist['img']);
            }
            else {
                $newClosEtdata['img']=$exist['img'];
            }
            //插入数据库
            $row=$this->closetM->where("id=".$id)->save($newClosEtdata);
            $newClosEtdata['id']=$id;
            //把图片组装成前端可以直接访问的http全路径
            $newClosEtdata['img']=ic_getUploadImgFullPath($this->imgSaveSubFolder,$newClosEtdata['img'],$newClosEtdata['create_time']);
            
            $this->customReturn(1,$newClosEtdata);
        } catch (\Exception $exc) {
            $succ = $exc->getCode() == 1 ? -1 : $exc->getCode();
            $datas = array('errcode' => $succ, 'errmsg' => $exc->getMessage());
            $this->customReturn($succ, $datas);
        }  
    }


    public function mattingImg($imgBase64=false){
        if(!$imgBase64){
            $imgBase64=I('base64str');
            $startPos=stripos($imgBase64, ";base64,");
            if($startPos<=0){
                throw new \Exception("上传图片格式无效(!base64),请重试或联系管理员", -215);
            }
            $imgBase64=substr($imgBase64, $startPos+8);
        }

        $re = $this->callMattingApi($imgBase64);
        $succ=0;
        $reArr=json_decode($re,true);

        if($reArr){
            if(array_key_exists("result", $reArr)){
                if(array_key_exists("resultCode", $reArr['result'])){
                    if($reArr['result']['resultCode']==1){
                        $re=$reArr['result']['resultObject'];
                        //微信不支持显示taobao域名下的图片，这里要获取到本地
                        // $tmparr=explode("/", $re);
                        // $lenaa=count($tmparr);
                        // $filename=$tmparr[$lenaa-1];
                        // $filename=time().$filename;
                        // $aaa=$this->curl_file_get_contents($re);
                        // $ctime=time();
                        // $subPath=ic_getUploadImgServerPath('test',$ctime);
                        // if(!is_dir($subPath)) {
                        //     // 检查目录是否编码后的
                        //     if(is_dir(base64_decode($subPath))) {
                        //         $subPath   =   base64_decode($subPath);
                        //     }else{
                        //         // 尝试创建目录
                        //         if(!mkdir($subPath)){
                        //             throw new \Exception("保存目录不存在，请联系管理员", -215);
                        //         }
                        //     }
                        // }else {
                        //     if(!is_writeable($subPath)) {
                        //        throw new \Exception("保存目录不可写，请联系管理员", -333);
                        //     }
                        // }
                        // file_put_contents($subPath."/".$filename, $aaa);
                        // $re=ic_getUploadImgFullPath('test',$filename,$ctime);
                        $succ=1;
                    }
                }
            }
        }
        $this->customReturn($succ,$re);
    }

    public function mattingImgFix(){
        $aa='/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wAARCABLAEsDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDW/wCC8f8AwXe+MOofGjXv2Mv2PPHlx4b8PeGbh9P8W+KdHuCl5ql6pxLbwzKcxQxnKEphnYNztAB/I+813U9a1GXVdb1Ke7urhy9xc3MzSSSMerMzEkn3NZGseJNV8Ua7eeJtbvXnvdQu5Lm7uJGy0krsWZie5JJP4063uNwwTzX3dDCU8LSU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Bl8tWZnkkbaF3krhcgL8xNeTJ/weE/sEvwP2d/iz/4Bab/APJlYPjf/g8Q/ZktbCT/AIVp+yR461C6wfKGt6nZWcefcxPMf0p1lnuIh7OUdHvokbuWMqx5eh+wOtazpfh3SbnXtc1GC0srK3ee7u7mUJHDEilmdmPCqACST0Ar+Sf/AIKf/tL6F+1p+3/8UPj54UuPN0fWvEjR6PNj/W2ltFHawyD2dIVf/gVeq/8ABQz/AIODP20/2/vDN38K0ew8AeBb4Fb7w54Zkcy6hHnIS5un+eRfVFCI3dTXxBbXPQE12ZbltTCXqVN30PQy+g6Dc5bs24LjPINWlueOtY8FwV+npVxXd1DKCQfRa9FxPbhU0Om/4LDf8E+PH/8AwTg/bi8WfCHXdBuIfDGp6lPqfgLVjCRDf6VLIzRhG6F4s+U65yGX0IJ+Y4JwQOa/tX/4KA/sjfs2ftf/ALOOu+DP2lPg/pPi2w07T57zTV1BHWWzuFjJEkM0bLJE3AyUYZHByK/jP+PfhbQvA/xu8S+EfC1j9l07T9Xmhs7fzWfy0DYA3OSx+pJNdWS5m8wocs170bXfc+GT5WYtvce9XYLgDGDx3rIgJ45q5AzYHNepKKOqnNmzBOMA5/GrtvcYOM1j2zNkDNXbck8Z71zySR2QmzYt7jBzmrsE4Izmsi2JI5PartszZHPpXPJHZCTN/QrLU9d1K30TR7Ca7vLudYbS1toi8k0jHCoqryzEkAAckmv6Bf2Jf+Dcj4QH9lXwVdftL2Utt45utKN14hs1jB+zSyyvKsLc/eSN0Rv9pTXL/wDBsJ+xr+zDrHwcP7T2t/B3S7/x3Y3QSw8Q37SzyWuQfmiSRzHE3+2qhvev2Er5XNMxqRreyp6W3fc48VjailyQ0sf/2Q==';
        $this->mattingImg($aa);
    }

    private function curl_file_get_contents($durl){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $durl);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
        curl_setopt($ch, CURLOPT_REFERER,_REFERER_);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $r = curl_exec($ch);
        curl_close($ch);
        return $r;
    }

    private function callMattingApi($imgBase64) {
        $host = "https://dm-71.data.aliyun.com";
        $path = "/rest/160601/int_image/matting.json";
        $method = "POST";
        $appcode = "310ed976487b42de9ed19368b753cd38";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        //根据API的要求，定义相对应的Content-Type
        array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
        // array_push($headers, "X-Ca-Request-Mode".":"."debug");
        // array_push($headers, "X-Ca-Stage".":"."TEST");
        $querys = "";
        $bodys = '{"image": {"dataType": 10, "dataValue": "'.$imgBase64.'"}}';
        $url = $host . $path;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
            // dump('no ssl');
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
        ///DEBUG
        // dump($url);
        // dump($headers);
        // dump($bodys);
        $re = (curl_exec($curl));
        curl_close($curl);
        
        return $re;

    }

    private function delMulti($serverPath,$delName){
        @unlink($serverPath.$delName);
        @unlink($serverPath."m_".$delName);
        @unlink($serverPath."s_".$delName);
    }

    private function checkImgFile($img,$ext='jpg'){
        $imginfo = getimagesize($img);
        // dump($imginfo);
        // array(7) {
        //   [0] => int(414)
        //   [1] => int(736)
        //   [2] => int(2)
        //   [3] => string(24) "width="414" height="736""
        //   ["bits"] => int(8)
        //   ["channels"] => int(3)
        //   ["mime"] => string(10) "image/jpeg"
        // }
        if(empty($imginfo) || ($ext == 'gif' && empty($imginfo['bits']))){
            throw new \Exception("上传图片格式无效,请联系管理员", -48);
        }
    }





}
